package com.example.demo3.config;


import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.SourceLocation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Aspect
@Slf4j
@Component
public class  LogAspect {



        @Autowired
        private JdbcTemplate jdbcTemplate;
    
    
    
    
        /*
         * 这是一个切入点
         * */
        @Pointcut("@annotation(com.example.demo3.config.MyLog)")
        public void cutMethod(){

        }
        /**
         * 切点之前
         */
        @Before("cutMethod()")
        public void before(JoinPoint joinPoint) throws Throwable {
            Object[] args = joinPoint.getArgs();

            String kind = joinPoint.getKind();
            Signature signature = joinPoint.getSignature();
            Object target = joinPoint.getTarget();
            Object aThis = joinPoint.getThis();
            SourceLocation sourceLocation = joinPoint.getSourceLocation();
            JoinPoint.StaticPart staticPart = joinPoint.getStaticPart();
            List<Map<String, Object>> list = jdbcTemplate.queryForList("select * from sys_menu");
            log.info("============ before ==========");
        }

        /**
         * 切点之后
         */
        @After("cutMethod()")
        public void after() throws Throwable {
            log.info("============ after ==========");
        }

        /**
         * 切点返回内容后
         */
        @AfterReturning("cutMethod()")
        public void afterReturning() throws Throwable {
            log.info("============ afterReturning ==========");
        }

        /**
         * 切点抛出异常后
         */
        @AfterThrowing("cutMethod()")
        public void afterThrowing() throws Throwable {
            log.info("============ afterThrowing ==========");
        }


        @Around("cutMethod() && @annotation(myAnnotation)")
        public Object around(ProceedingJoinPoint point, MyLog myAnnotation) throws Throwable {

            log.info("============ around1 ==========");
            Object  obj= point.proceed(point.getArgs());
            log.info("============ around2 ==========");
            return obj;
        }





}
